# Copyright lowRISC contributors (OpenTitan project).
# Licensed under the Apache License, Version 2.0, see LICENSE for details.
# SPDX-License-Identifier: Apache-2.0

module(name = "lowrisc_opentitan")

# Dependencies:
bazel_dep(name = "abseil-cpp", version = "20240116.1")
bazel_dep(name = "bazel_skylib", version = "1.7.1")
bazel_dep(name = "googletest", version = "1.14.0.bcr.1")
bazel_dep(name = "platforms", version = "0.0.11")
bazel_dep(name = "rules_cc", version = "0.1.2")
bazel_dep(name = "rules_fuzzing", version = "0.5.2")
bazel_dep(name = "rules_pkg", version = "1.0.1")

# Dev dependencies:
bazel_dep(
    name = "aspect_rules_lint",
    version = "1.2.2",
    dev_dependency = True,
)
bazel_dep(
    name = "buildifier_prebuilt",
    version = "6.4.0",
    dev_dependency = True,
)
bazel_dep(
    name = "lowrisc_misc_linters",
    dev_dependency = True,
)
archive_override(
    module_name = "lowrisc_misc_linters",
    integrity = "sha256-eRFiSjD638NuOjDDScMuEKVYpmotDY0yX3jxYL/d3ac=",
    strip_prefix = "misc-linters-20250217_01",
    urls = ["https://github.com/lowRISC/misc-linters/archive/refs/tags/20250217_01.tar.gz"],
)

archive_override(
    module_name = "rules_multitool",
    sha256 = "63e8b3d7999574a62ccf49d3234390c584a11ac90392eb16d63b964584c1af6c",
    strip_prefix = "rules_multitool-temp-fork-0.4",
    urls = ["https://github.com/jwnrt/rules_multitool/archive/refs/tags/temp-fork-0.4.tar.gz"],
)

################### originally from //third_party/rust:rust.MODULE.bazel

# Dependencies:
bazel_dep(name = "rules_rust", version = "0.59.2")
bazel_dep(name = "rules_rust_bindgen", version = "0.59.2")
bazel_dep(name = "rules_rust_mdbook", version = "0.59.2")
bazel_dep(name = "toolchains_llvm", version = "1.1.2")

# Overrides and patches:
single_version_override(
    module_name = "rules_rust",
    patch_strip = 1,
    patches = [
        "@lowrisc_opentitan//third_party/rust/patches:rules_rust.extra_rustc_toolchain_dirs.patch",
    ],
    version = "0.59.2",
)

single_version_override(
    module_name = "rules_rust_bindgen",
    patch_strip = 3,
    patches = [
        "@lowrisc_opentitan//third_party/rust/patches:rules_rust.bindgen_static_lib.patch",
    ],
    version = "0.59.2",
)

# Rust toolchain:
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.repository_set(
    name = "rust_host",
    edition = "2024",
    exec_triple = "x86_64-unknown-linux-gnu",
    sha256s = {
        "2025-01-03/rustc-nightly-x86_64-unknown-linux-gnu.tar.xz": "a7e713b2c38d2c16a2025d228480909a2281c91ad8fd225b1dacc3eda933724c",
        "2025-01-03/clippy-nightly-x86_64-unknown-linux-gnu.tar.xz": "5d04b1e1a23c054cbb1775a32ece3d09f7bb158601b82a038f51bb998fce8ee8",
        "2025-01-03/cargo-nightly-x86_64-unknown-linux-gnu.tar.xz": "e28f21e048490c2cc212169799b5ac3a01651e6946aca2f120adf0be6f3a70d9",
        "2025-01-03/llvm-tools-nightly-x86_64-unknown-linux-gnu.tar.xz": "67e9e52780680c3a4b0dadc138864a9da0fb99a4af882d3477b90c8b2efe474c",
        "2025-01-03/rust-std-nightly-x86_64-unknown-linux-gnu.tar.xz": "a5f96b464ace329963eef9e358303a17b5544cbd49b450474f4bc16cae0cc191",
    },
    target_compatible_with = [
        "@platforms//cpu:x86_64",
        "@platforms//os:linux",
    ],
    target_triple = "x86_64-unknown-linux-gnu",
    # Use nightly that Rust 1.85 branches from.
    versions = ["nightly/2025-01-03"],
)
rust.repository_set(
    name = "rust_tock",
    edition = "2021",
    exec_triple = "x86_64-unknown-linux-gnu",
    sha256s = {
        "2023-07-30/rustc-nightly-x86_64-unknown-linux-gnu.tar.xz": "ecdee8821a57efbb699b7e3aa4cbfbd60b7970bce89a8cfb9bc7d65b9058ee42",
        "2023-07-30/clippy-nightly-x86_64-unknown-linux-gnu.tar.xz": "76ee5aac81d1348bfebd3d94d5fb65c3f4ea0cf5fc2de834926f93772547380c",
        "2023-07-30/cargo-nightly-x86_64-unknown-linux-gnu.tar.xz": "4ddb3ed2dd2acedf9097f4a1fe17b8cd571fdd7c9a49b1e31c228a284ec95049",
        "2023-07-30/llvm-tools-nightly-x86_64-unknown-linux-gnu.tar.xz": "dc71b9ae6a4a4b9fa259724b29f4ad19467197ced89a8aad675f5af112c4fb77",
        "2023-07-30/rust-std-nightly-riscv32imc-unknown-none-elf.tar.xz": "9790d50d4510443bbf4c13b68227a273345d28b84d29372bc5f5ea2d14d05f2d",
        "2023-07-30/rust-std-nightly-x86_64-unknown-linux-gnu.tar.xz": "b5a589a243923c5fa2a1f08e7b902bb0a64ae08010067b9074501a6e1fb8b042",
    },
    target_compatible_with = [
        "@platforms//cpu:riscv32",
    ],
    target_triple = "riscv32imc-unknown-none-elf",
    # For RISC-V toolchain, we currently don't support LLVM 16+ due to old binutils version.
    # Rust updated its LLVM 17 on 2023-08-08.
    versions = ["nightly/2023-07-30"],
)
use_repo(rust, "rust_toolchains")

register_toolchains("@rust_toolchains//:all")

# Rust crates:
crate = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
crate.from_cargo(
    name = "crate_index",
    cargo_lockfile = "//third_party/rust:Cargo.lock",
    manifests = ["//third_party/rust:Cargo.toml"],
    # By default rules_rust will analyze dependencies for all supported target triples, which is very slow.
    # Restrict to ones that we're interested in. See https://github.com/bazelbuild/rules_rust/issues/2876
    supported_platform_triples = [
        "x86_64-unknown-linux-gnu",
    ],
)
crate.annotation(
    crate = "libudev-sys",
    patch_args = ["-p1"],
    patches = ["@lowrisc_opentitan//third_party/rust/patches:libudev-sys-0.1.4.patch"],
    repositories = ["crate_index"],
)
crate.annotation(
    crate = "cryptoki",
    patch_args = ["-p2"],
    patches = [
        "@lowrisc_opentitan//third_party/rust/patches:cryptoki-vendor-defined-mechanism-type.patch",
        "@lowrisc_opentitan//third_party/rust/patches:cryptoki-profile.patch",
    ],
    repositories = ["crate_index"],
)
crate.annotation(
    additive_build_file_content = """
        filegroup(
            name = "binding_srcs",
            srcs = [
                "src/lib.rs",
                "src/bindings/x86_64-unknown-linux-gnu.rs",
            ],
        )
    """,
    crate = "cryptoki-sys",
    extra_aliased_targets = {
        "cryptoki-sys-binding-srcs": "binding_srcs",
    },
    repositories = ["crate_index"],
)
crate.annotation(
    build_script_env = {
        "PKG_CONFIG_PATH": "$(OPENSSL_PKG_CONFIG_PATH)",
        "OPENSSL_STATIC": "1",
    },
    build_script_toolchains = ["@lowrisc_opentitan//third_party/rust:openssl_pkg_config_path"],
    crate = "openssl-sys",
    repositories = ["crate_index"],
)
use_repo(crate, "crate_index")

# mdbook (separate because it has a lot of dependencies):
crate.from_cargo(
    name = "mdbook_index",
    cargo_lockfile = "//third_party/mdbook:Cargo.lock",
    manifests = ["//third_party/mdbook:Cargo.toml"],
    supported_platform_triples = [
        "x86_64-unknown-linux-gnu",
    ],
)
crate.annotation(
    crate = "mdbook",
    gen_binaries = ["mdbook"],
    patch_args = ["-p1"],
    patches = [
        "@lowrisc_opentitan//third_party/mdbook/patches:mdbook-mdignore.patch",
    ],
    repositories = ["mdbook_index"],
)
use_repo(crate, "mdbook_index")

# Rust bindgen toolchain:
register_toolchains("//third_party/rust:bindgen_toolchain")

# LLVM toolchain used by bindgen for `libclang` (not for device software):
llvm = use_extension("@toolchains_llvm//toolchain/extensions:llvm.bzl", "llvm")
llvm.toolchain(
    name = "llvm_toolchain",
    llvm_versions = {"": "10.0.0"},
    sha256 = {"": "b25f592a0c00686f03e3b7db68ca6dc87418f681f4ead4df4745a01d9be63843"},
    strip_prefix = {"": "clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04"},
    urls = {"": ["https://github.com/llvm/llvm-project/releases/download/llvmorg-10.0.0/clang+llvm-10.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz"]},
)
use_repo(llvm, "llvm_toolchain_llvm")

################### originally from "//third_party/python:python.MODULE.bazel

bazel_dep(name = "rules_python", version = "1.2.0")

# Python toolchain:
python = use_extension("@rules_python//python/extensions:python.bzl", "python")
python.toolchain(
    # Required because CI containers currently run as the `root` user.
    # We lose caching of `.pyc` files as a result.
    # See <https://github.com/bazelbuild/rules_python/pull/713>.
    ignore_root_user_error = True,
    is_default = True,
    python_version = "3.10",
)
use_repo(python, "pythons_hub")

register_toolchains("@pythons_hub//:all")

# We occasionally access the toolchain repositories directly to use interpreters.
use_repo(python, python3 = "python_3_10", python3_host = "python_3_10_host")

# Pip dependencies:
pip = use_extension("@rules_python//python/extensions:pip.bzl", "pip")
pip.parse(
    hub_name = "ot_python_deps",
    python_version = "3.10",
    requirements_lock = "//:python-requirements.txt",
)
use_repo(pip, "ot_python_deps")

################### originally from //third_party/foreign_cc:foreign_cc.MODULE.bazel

# Dependencies:
bazel_dep(name = "rules_foreign_cc", version = "0.9.0")

# Overrides and patches:
single_version_override(
    module_name = "rules_foreign_cc",
    patches = [
        # Patch to remove all build log file output when using rules_foreign_cc
        # toolchains to ensure deterministic Bazel builds. See upstream issue:
        # https://github.com/bazel-contrib/rules_foreign_cc/issues/1313
        "@lowrisc_opentitan//third_party/foreign_cc/patches:rules_foreign_cc.remove_log_output.patch",
    ],
    version = "0.9.0",
)

################### originally from //third_party/tock:tock.MODULE.bazel

# Tock crates (must be separated to avoid them depending on `std`):
crate.from_cargo(
    name = "tock_index",
    cargo_lockfile = "//third_party/tock:Cargo.lock",
    manifests = ["//third_party/tock:Cargo.toml"],
    supported_platform_triples = [
        "riscv32imc-unknown-none-elf",
    ],
)
use_repo(crate, "tock_index")

# elf2tab crates (pulled directly using elf2tab's Cargo.toml/Cargo.lock files)
crate.from_cargo(
    name = "elf2tab_index",
    cargo_lockfile = "@elf2tab//:Cargo.lock",
    manifests = ["@elf2tab//:Cargo.toml"],
    supported_platform_triples = [
        "x86_64-unknown-linux-gnu",
    ],
)
use_repo(crate, "elf2tab_index")

tock = use_extension("//third_party/tock:extensions.bzl", "tock")
use_repo(tock, "elf2tab", "libtock", "tock")

###################

# Repository rules:
bitstreams_repo = use_repo_rule("//rules:bitstreams.bzl", "bitstreams_repo")

bitstreams_repo(name = "bitstreams")

nonhermetic_repo = use_repo_rule("//rules:nonhermetic.bzl", "nonhermetic_repo")

nonhermetic_repo(name = "nonhermetic")

# Source repositories (not Bazel-aware):
github_tools = use_extension("//third_party/github:extensions.bzl", "github_tools")
use_repo(github_tools, "com_github_gh")

lychee = use_extension("//third_party/lychee:extensions.bzl", "lychee")
use_repo(lychee, "lychee")

serde_annotate = use_extension("//third_party/rust:extensions.bzl", "serde_annotate")
use_repo(serde_annotate, "lowrisc_serde_annotate")

shellcheck = use_extension("//third_party/shellcheck:extensions.bzl", "shellcheck")
use_repo(shellcheck, "shellcheck")

openocd = use_extension("//third_party/openocd:extensions.bzl", "openocd")
use_repo(openocd, "openocd")

freertos = use_extension("//third_party/freertos:extensions.bzl", "freertos")
use_repo(freertos, "freertos")

llvm_compiler_rt = use_extension("//third_party/llvm_compiler_rt:extensions.bzl", "llvm_compiler_rt")
use_repo(llvm_compiler_rt, "llvm_compiler_rt")

riscv_compliance = use_extension("//third_party/riscv-compliance:extensions.bzl", "riscv_compliance")
use_repo(riscv_compliance, "riscv-compliance")

coremark = use_extension("//third_party/coremark:extensions.bzl", "coremark")
use_repo(coremark, "coremark")

xkcp = use_extension("//third_party/xkcp:extensions.bzl", "xkcp")
use_repo(xkcp, "xkcp")

hsm = use_extension("//third_party/hsm:extensions.bzl", "hsm")
use_repo(hsm, "cloud_kms_hsm", "opensc", "sc_hsm", "softhsm2")

nist_cavp = use_extension("//third_party/nist_cavp_testvectors:extensions.bzl", "nist_cavp")
use_repo(
    nist_cavp,
    "nist_cavp_aes_gcm",
    "nist_cavp_aes_kat",
    "nist_cavp_aes_kw_sp_800_38f",
    "nist_cavp_drbg_sp_800_90a_root",
    "nist_cavp_ecdh_sp_800_56a",
    "nist_cavp_ecdsa_fips_186_4",
    "nist_cavp_hmac_fips_198_1",
    "nist_cavp_rsa_fips_186_3",
    "nist_cavp_sha2_fips_180_4",
    "nist_cavp_sha3_fips_202",
    "nist_cavp_shake_fips_202",
)

wycheproof = use_extension("//third_party/wycheproof:extensions.bzl", "wycheproof")
use_repo(wycheproof, "wycheproof")

sphincsplus = use_extension("//third_party/sphincsplus:extensions.bzl", "sphincsplus")
use_repo(
    sphincsplus,
    "sphincsplus_fips205_ipd",
    "sphincsplus_sha256_kat",
    "sphincsplus_shake256_kat",
)

open_dice = use_extension("//third_party/open-dice:extensions.bzl", "open_dice")
use_repo(open_dice, "open-dice")

hyperdebug = use_extension("//third_party/hyperdebug:extensions.bzl", "hyperdebug")
use_repo(hyperdebug, "hyperdebug_firmware")

lowrisc_rv32imcb_toolchain = use_extension("//third_party/lowrisc:extensions.bzl", "lowrisc_rv32imcb_toolchain")
use_repo(lowrisc_rv32imcb_toolchain, "lowrisc_rv32imcb_toolchain")

qemu = use_extension("//third_party/qemu:extensions.bzl", "qemu")
use_repo(qemu, "qemu_opentitan")

doxygen = use_extension("//third_party/doxygen:extensions.bzl", "doxygen")
use_repo(doxygen, "doxygen")

# Extension for linking in externally managed test and provisioning customizations
# for both secure/non-secure manufacturer domains.
hooks = use_extension("//rules:extensions.bzl", "hooks")
hooks.repo(
    name = "manufacturer_test_hooks",
    dummy = "@lowrisc_opentitan//:sw/device/tests/closed_source",
    env = "MANUFACTURER_HOOKS_DIR",
)
hooks.repo(
    name = "secure_manufacturer_test_hooks",
    dummy = "@lowrisc_opentitan//:sw/device/tests/closed_source",
    env = "SECURE_MANUFACTURER_HOOKS_DIR",
)
hooks.repo(
    name = "provisioning_exts",
    dummy = "@lowrisc_opentitan//:sw/device/silicon_creator/manuf/extensions",
    env = "PROV_EXTS_DIR",
)
hooks.repo(
    name = "rom_hooks",
    dummy = "@lowrisc_opentitan//:sw/device/silicon_creator/rom/hooks",
    env = "ROM_HOOKS_DIR",
)
use_repo(
    hooks,
    "manufacturer_test_hooks",
    "provisioning_exts",
    "rom_hooks",
    "secure_manufacturer_test_hooks",
)

register_toolchains("//rules/opentitan:localtools")

register_toolchains("//toolchain:cc_toolchain_opentitan")
